﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongMau;

namespace QuanLyKho.Control
{
    public partial class Phongmau_DkLoaihang : UserControl
    {
        public Phongmau_DkLoaihang()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            gridLoaiHang.DataSource = DataAll.SelectAll("PhongMau_LoaiHang", "*", "");
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            //gvNhomHang.BestFitColumns();
            gvLoaiHang.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvLoaiHang.OptionsBehavior.Editable = false;
            gvLoaiHang.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvLoaiHang.OptionsSelection.MultiSelect = true;
            gvLoaiHang.OptionsView.ShowGroupPanel = false;
            gvLoaiHang.OptionsView.ColumnAutoWidth = true;
            gvLoaiHang.OptionsView.ShowFooter = true;
            gvLoaiHang.OptionsFind.AlwaysVisible = true;
            gvLoaiHang.Columns["IDLoaiHang"].Visible = false;
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            var themloaihang = new Phongmau_Themloaihang();
            themloaihang.ShowDialog();
            LoadData();
        }

        private void gvLoaiHang_DoubleClick(object sender, EventArgs e)
        {
            if (gvLoaiHang.FocusedRowHandle >= 0)
            {
                Phongmau_Themloaihang.MaID = gvLoaiHang.Columns["IDLoaiHang"].View.GetRowCellDisplayText(gvLoaiHang.FocusedRowHandle, "IDLoaiHang");
                var nhomhang = new Phongmau_Themloaihang();
                nhomhang.update = true;
                nhomhang.ShowDialog();
                LoadData();
                Phongmau_Themloaihang.MaID = "0";
            }
        }

        private void btnXoa_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvLoaiHang.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa loại hàng này ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Delete("PhongMau_LoaiHang", "IDLoaiHang = " + dataview[gvLoaiHang.FocusedRowHandle]["IDLoaiHang"]);
                MessageBox.Show(message.Equals("success") ? "Xóa thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            LoadData();
        }
    }
}
